package com.techthinker.dreamBuilder.Setting;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import com.techthinker.dreamBuilder.Database.DatabaseHelper;
import com.techthinker.dreamBuilder.Define.Constant;
import com.techthinker.dreamBuilder.Dictionary.DictionaryDAL;
import com.techthinker.dreamBuilder.Entity.Trace;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class TraceSettingDAL {

	private static final String KEY_TASK_ID = "task_id";
	private static final String KEY_DIC_ID = "dictionary_id";
	private static final String KEY_TRACE_NUM = "trace_num";
	private static final String KEY_START_STEP = "startStep";
	private static final String KEY_END_STEP = "endStep";
	private static final String KEY_LAST_STEP = "lastStep";
	private static final String KEY_DATE = "date";
	private static final String KEY_ROW_ID = "trace_id";

	private final Context mCtx;

	private DatabaseHelper mDbHelper = null;
	private SQLiteDatabase mDb = null;

	public TraceSettingDAL(Context ctx) {
		this.mCtx = ctx;
	}

	public TraceSettingDAL Open() {
		this.mDbHelper = new DatabaseHelper(mCtx, Constant.DB_NAME);
		this.mDb = this.mDbHelper.getWritableDatabase();
		return this;
	}

	public void Close() {
		this.mDbHelper.close();
		this.mDbHelper = null;
		this.mDb = null;
	}

	public boolean isClosed() {
		if (this.mDbHelper == null)
			return true;
		else
			return false;
	}

	// create a trace item
	public long createTrace(Trace trace) {
		ContentValues cv = new ContentValues();
		cv.put(this.KEY_DIC_ID, trace.getDictionary().getDictionary_id());
		cv.put(this.KEY_TASK_ID, trace.getTask().getTask_id());
		cv.put(this.KEY_TRACE_NUM, trace.getTrace_num());
		cv.put(this.KEY_START_STEP, trace.getStartStep());
		cv.put(this.KEY_END_STEP, trace.getEndStep());
		cv.put(this.KEY_LAST_STEP, trace.getLastStep());
		cv.put(this.KEY_DATE, trace.getDate().toString());

		return this.mDb.insert(Constant.TABLE_TRACE, null, cv);
	}

	// update a trace item
	public boolean updateTrace(Trace trace) {
		ContentValues cv = new ContentValues();
		cv.put(this.KEY_DIC_ID, trace.getDictionary().getDictionary_id());
		cv.put(this.KEY_TASK_ID, trace.getTask().getTask_id());
		cv.put(this.KEY_TRACE_NUM, trace.getTrace_num());
		cv.put(this.KEY_START_STEP, trace.getStartStep());
		cv.put(this.KEY_END_STEP, trace.getEndStep());
		cv.put(this.KEY_LAST_STEP, trace.getLastStep());
		cv.put(this.KEY_DATE, trace.getDate().toString());

		return mDb.update(Constant.TABLE_TRACE, cv,
				KEY_ROW_ID + "=" + trace.getTrace_id(), null) > 0;
	}

	// delete a trace item
	public boolean deleteTrace(Trace trace) {
		return mDb.delete(Constant.TABLE_TRACE,
				KEY_ROW_ID + "=" + trace.getTrace_id(), null) > 0;
	}

	//
	public boolean deleteTrace(int trace_id) {
		return mDb.delete(Constant.TABLE_TRACE, KEY_ROW_ID + "=" + trace_id,
				null) > 0;
	}

	// retrieve all trace
	public List<Trace> fetchAllTraces() {
		Cursor mCursor = mDb.query(Constant.TABLE_TRACE, new String[] {
				KEY_ROW_ID, KEY_TASK_ID, KEY_DIC_ID, KEY_TRACE_NUM,
				KEY_START_STEP, KEY_END_STEP, KEY_LAST_STEP, KEY_DATE }, null,
				null, null, null, null);
		List<Trace> alllists = new ArrayList<Trace>();
		while (mCursor.moveToNext()) {
			Trace trace = new Trace();
			trace.setTrace_id((mCursor.getInt(mCursor
					.getColumnIndex(KEY_ROW_ID))));
			// fetch the related dictionary
			{
				TaskSettingDAL taskDAL = new TaskSettingDAL(this.mCtx);
				taskDAL.Open();
				trace.setTask(taskDAL.fetchTaskById(mCursor.getInt(mCursor
						.getColumnIndex(KEY_TASK_ID))));
				taskDAL.Close();
				DictionaryDAL dicDAL = new DictionaryDAL(mCtx);
				dicDAL.open();
				trace.setDictionary(dicDAL.fetchDictionaryById(mCursor
						.getInt(mCursor.getColumnIndex(KEY_DIC_ID))));
				dicDAL.close();
			}
			trace.setStartStep(mCursor.getInt(mCursor
					.getColumnIndex(KEY_START_STEP)));
			trace.setStartStep(mCursor.getInt(mCursor
					.getColumnIndex(KEY_END_STEP)));
			trace.setStartStep(mCursor.getInt(mCursor
					.getColumnIndex(KEY_LAST_STEP)));
			trace.setTrace_num(mCursor.getInt(mCursor
					.getColumnIndex(KEY_TRACE_NUM)));
			trace.setDate(Date.valueOf(mCursor.getString(mCursor
					.getColumnIndex(KEY_DATE))));
			alllists.add(trace);
		}
		mCursor.close();
		return alllists;
	}

	// retrieve all traces to cursor
	public Cursor fetchAllTracessToCursor() {

		return mDb.query(Constant.TABLE_TRACE, new String[] { KEY_ROW_ID,
				KEY_TASK_ID, KEY_DIC_ID, KEY_TRACE_NUM, KEY_START_STEP,
				KEY_END_STEP, KEY_LAST_STEP, KEY_DATE }, null, null, null,
				null, null);
	}

	// retrieve a trace item by id
	public Trace fetchTraceById(long rowId) throws SQLException {

		Cursor mCursor = mDb.query(true, Constant.TABLE_TRACE, new String[] {
				KEY_ROW_ID, KEY_TASK_ID, KEY_DIC_ID, KEY_TRACE_NUM,
				KEY_START_STEP, KEY_END_STEP, KEY_LAST_STEP, KEY_DATE },
				KEY_ROW_ID + "=" + rowId, null, null, null, null, null);

		if (mCursor.moveToFirst()) {
			Trace trace = new Trace();
			trace.setTrace_id((mCursor.getInt(mCursor
					.getColumnIndex(KEY_ROW_ID))));
			// fetch the related dictionary
			{
				TaskSettingDAL taskDAL = new TaskSettingDAL(this.mCtx);
				taskDAL.Open();
				trace.setTask(taskDAL.fetchTaskById(mCursor.getInt(mCursor
						.getColumnIndex(KEY_TASK_ID))));
				taskDAL.Close();
				DictionaryDAL dicDAL = new DictionaryDAL(mCtx);
				dicDAL.open();
				trace.setDictionary(dicDAL.fetchDictionaryById(mCursor
						.getInt(mCursor.getColumnIndex(KEY_DIC_ID))));
				dicDAL.close();
			}
			trace.setStartStep(mCursor.getInt(mCursor
					.getColumnIndex(KEY_START_STEP)));
			trace.setStartStep(mCursor.getInt(mCursor
					.getColumnIndex(KEY_END_STEP)));
			trace.setStartStep(mCursor.getInt(mCursor
					.getColumnIndex(KEY_LAST_STEP)));
			trace.setTrace_num(mCursor.getInt(mCursor
					.getColumnIndex(KEY_TRACE_NUM)));
			trace.setDate(Date.valueOf(mCursor.getString(mCursor
					.getColumnIndex(KEY_DATE))));
			mCursor.close();
			return trace;
		}
		mCursor.close();
		return null;

	}

}
